// 67. Add Binary

function addBinary(a, b) {
    let result = [];
    let longString;
    let longLength;
    let shortIndex;
    let shortString;
    let zeroizeIndex = 1;

    if (a.length < b.length) {
        longString = b;
        longLength = b.length;
        shortIndex = a.length;
        shortString = a;
    } else {
        longString = a;
        longLength = a.length;
        shortIndex = b.length;
        shortString = b;
    }

    for (let i = longLength - 1; i >= 0; i--) {
        console.log("momo", result);
       
        let element = Number(longString[i]);
        let shortElement;
        let sum;
        let resultFirst;
        let resultElement;

        if (shortIndex > 0) {
            shortIndex--;
            shortElement = Number(shortString[shortIndex]);
        } else {
            shortElement = 0;
        }
        
        if (zeroizeIndex > result.length) {
            sum = element + shortElement;
        } else {
            resultFirst = result.shift();
            resultElement = resultFirst === undefined ? 0 : resultFirst;
            sum = element + shortElement + resultElement;
        }

        console.log("iii", element, shortElement, resultElement, zeroizeIndex);


        switch (sum) {
            case 3:
                if (zeroizeIndex > result.length) {
                    result.unshift(1);
                    result.unshift(1);
                } else {
                    result.unshift(0);
                    result.unshift(1);
                    result.unshift(1);
                }
                
                break;
            case 2:
                result.unshift(0);
                result.unshift(1);
                break;
            case 1:
                result.unshift(1);
                break;
            case 0:
                result.unshift(0);
                break;
            default:
                break;
        }

        zeroizeIndex++;
    }

    return result.join("");
}

console.log("result", addBinary("1111", "1111"));
